package com.koushikdutta.async;

import android.os.Build.VERSION;
import android.os.Handler;
import android.util.Log;
import com.koushikdutta.async.callback.ConnectCallback;
import com.koushikdutta.async.callback.ListenCallback;
import com.koushikdutta.async.future.Cancellable;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.async.future.SimpleFuture;
import com.koushikdutta.async.future.TransformFuture;
import com.koushikdutta.async.util.StreamUtility;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class AsyncServer {
    static final /* synthetic */ boolean $assertionsDisabled;
    static AsyncServer mInstance = new AsyncServer();
    static final WeakHashMap<Thread, AsyncServer> mServers = new WeakHashMap();
    private static ExecutorService synchronousWorkers = newSynchronousWorkers();
    Thread mAffinity;
    String mName;
    PriorityQueue<Scheduled> mQueue;
    private SelectorWrapper mSelector;

    private static class AsyncSelectorException extends IOException {
        public AsyncSelectorException(Exception e) {
            super(e);
        }
    }

    private class ConnectFuture extends SimpleFuture<AsyncNetworkSocket> {
        ConnectCallback callback;
        SocketChannel socket;

        private ConnectFuture() {
        }

        protected void cancelCleanup() {
            super.cancelCleanup();
            try {
                if (this.socket != null) {
                    this.socket.close();
                }
            } catch (IOException e) {
            }
        }
    }

    private static class NamedThreadFactory implements ThreadFactory {
        private final ThreadGroup group;
        private final String namePrefix;
        private final AtomicInteger threadNumber = new AtomicInteger(1);

        NamedThreadFactory(String namePrefix) {
            SecurityManager s = System.getSecurityManager();
            this.group = s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
            this.namePrefix = namePrefix;
        }

        public Thread newThread(Runnable r) {
            Thread t = new Thread(this.group, r, this.namePrefix + this.threadNumber.getAndIncrement(), 0);
            if (t.isDaemon()) {
                t.setDaemon(false);
            }
            if (t.getPriority() != 5) {
                t.setPriority(5);
            }
            return t;
        }
    }

    private static class ObjectHolder<T> {
        T held;

        private ObjectHolder() {
        }
    }

    private static class RunnableWrapper implements Runnable {
        Handler handler;
        boolean hasRun;
        Runnable runnable;
        ThreadQueue threadQueue;

        private RunnableWrapper() {
        }

        /* JADX WARNING: inconsistent code. */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public void run() {
            /*
            r3 = this;
            r2 = 0;
            monitor-enter(r3);
            r0 = r3.hasRun;	 Catch:{ all -> 0x0022 }
            if (r0 == 0) goto L_0x0008;
        L_0x0006:
            monitor-exit(r3);	 Catch:{ all -> 0x0022 }
        L_0x0007:
            return;
        L_0x0008:
            r0 = 1;
            r3.hasRun = r0;	 Catch:{ all -> 0x0022 }
            monitor-exit(r3);	 Catch:{ all -> 0x0022 }
            r0 = r3.runnable;	 Catch:{ all -> 0x0025 }
            r0.run();	 Catch:{ all -> 0x0025 }
            r0 = r3.threadQueue;
            r0.remove(r3);
            r0 = r3.handler;
            r0.removeCallbacks(r3);
            r3.threadQueue = r2;
            r3.handler = r2;
            r3.runnable = r2;
            goto L_0x0007;
        L_0x0022:
            r0 = move-exception;
            monitor-exit(r3);	 Catch:{ all -> 0x0022 }
            throw r0;
        L_0x0025:
            r0 = move-exception;
            r1 = r3.threadQueue;
            r1.remove(r3);
            r1 = r3.handler;
            r1.removeCallbacks(r3);
            r3.threadQueue = r2;
            r3.handler = r2;
            r3.runnable = r2;
            throw r0;
            */
            throw new UnsupportedOperationException("Method not decompiled: com.koushikdutta.async.AsyncServer.RunnableWrapper.run():void");
        }
    }

    private static class Scheduled {
        public Runnable runnable;
        public long time;

        public Scheduled(Runnable runnable, long time) {
            this.runnable = runnable;
            this.time = time;
        }
    }

    static class Scheduler implements Comparator<Scheduled> {
        public static Scheduler INSTANCE = new Scheduler();

        private Scheduler() {
        }

        public int compare(Scheduled s1, Scheduled s2) {
            if (s1.time == s2.time) {
                return 0;
            }
            if (s1.time > s2.time) {
                return 1;
            }
            return -1;
        }
    }

    static {
        boolean z;
        if (AsyncServer.class.desiredAssertionStatus()) {
            z = false;
        } else {
            z = true;
        }
        $assertionsDisabled = z;
        try {
            if (VERSION.SDK_INT <= 8) {
                System.setProperty("java.net.preferIPv4Stack", "true");
                System.setProperty("java.net.preferIPv6Addresses", "false");
            }
        } catch (Throwable th) {
        }
    }

    public static void post(Handler handler, Runnable runnable) {
        Runnable wrapper = new RunnableWrapper();
        ThreadQueue threadQueue = ThreadQueue.getOrCreateThreadQueue(handler.getLooper().getThread());
        wrapper.threadQueue = threadQueue;
        wrapper.handler = handler;
        wrapper.runnable = runnable;
        threadQueue.add(wrapper);
        handler.post(wrapper);
        threadQueue.queueSemaphore.release();
    }

    public static AsyncServer getDefault() {
        return mInstance;
    }

    public AsyncServer() {
        this(null);
    }

    public AsyncServer(String name) {
        this.mQueue = new PriorityQueue(1, Scheduler.INSTANCE);
        if (name == null) {
            name = "AsyncServer";
        }
        this.mName = name;
    }

    public void removeAllCallbacks(Object scheduled) {
        synchronized (this) {
            this.mQueue.remove(scheduled);
        }
    }

    private static void wakeup(final SelectorWrapper selector) {
        synchronousWorkers.execute(new Runnable() {
            public void run() {
                try {
                    selector.wakeupOnce();
                } catch (Exception e) {
                    Log.i("NIO", "Selector Exception? L Preview?");
                }
            }
        });
    }

    public Object postDelayed(Runnable runnable, long delay) {
        Scheduled s;
        synchronized (this) {
            long time;
            if (delay != 0) {
                time = System.currentTimeMillis() + delay;
            } else {
                time = (long) this.mQueue.size();
            }
            PriorityQueue priorityQueue = this.mQueue;
            s = new Scheduled(runnable, time);
            priorityQueue.add(s);
            if (this.mSelector == null) {
                run(true);
            }
            if (!isAffinityThread()) {
                wakeup(this.mSelector);
            }
        }
        return s;
    }

    public Object post(Runnable runnable) {
        return postDelayed(runnable, 0);
    }

    public void run(final Runnable runnable) {
        if (Thread.currentThread() == this.mAffinity) {
            post(runnable);
            lockAndRunQueue(this, this.mQueue);
            return;
        }
        final Semaphore semaphore = new Semaphore(0);
        post(new Runnable() {
            public void run() {
                runnable.run();
                semaphore.release();
            }
        });
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
            Log.e("NIO", "run", e);
        }
    }

    protected void onDataReceived(int transmitted) {
    }

    protected void onDataSent(int transmitted) {
    }

    public AsyncServerSocket listen(InetAddress host, int port, ListenCallback handler) {
        final ObjectHolder<AsyncServerSocket> holder = new ObjectHolder();
        final InetAddress inetAddress = host;
        final int i = port;
        final ListenCallback listenCallback = handler;
        run(new Runnable() {
            public void run() {
                IOException e;
                ServerSocketChannel closeableServer = null;
                ServerSocketChannelWrapper closeableWrapper = null;
                try {
                    closeableServer = ServerSocketChannel.open();
                    ServerSocketChannelWrapper closeableWrapper2 = new ServerSocketChannelWrapper(closeableServer);
                    final ServerSocketChannel server = closeableServer;
                    final ServerSocketChannelWrapper wrapper = closeableWrapper2;
                    try {
                        InetSocketAddress isa;
                        if (inetAddress == null) {
                            isa = new InetSocketAddress(i);
                        } else {
                            isa = new InetSocketAddress(inetAddress, i);
                        }
                        server.socket().bind(isa);
                        final SelectionKey key = wrapper.register(AsyncServer.this.mSelector.getSelector());
                        key.attach(listenCallback);
                        ListenCallback listenCallback = listenCallback;
                        ObjectHolder objectHolder = holder;
                        AsyncServerSocket anonymousClass1 = new AsyncServerSocket() {
                            public void stop() {
                                StreamUtility.closeQuietly(wrapper);
                                try {
                                    key.cancel();
                                } catch (Exception e) {
                                }
                            }
                        };
                        objectHolder.held = anonymousClass1;
                        listenCallback.onListening(anonymousClass1);
                        closeableWrapper = closeableWrapper2;
                    } catch (IOException e2) {
                        e = e2;
                        closeableWrapper = closeableWrapper2;
                        StreamUtility.closeQuietly(closeableWrapper, closeableServer);
                        listenCallback.onCompleted(e);
                    }
                } catch (IOException e3) {
                    e = e3;
                    StreamUtility.closeQuietly(closeableWrapper, closeableServer);
                    listenCallback.onCompleted(e);
                }
            }
        });
        return (AsyncServerSocket) holder.held;
    }

    private ConnectFuture connectResolvedInetSocketAddress(final InetSocketAddress address, final ConnectCallback callback) {
        final ConnectFuture cancel = new ConnectFuture();
        if ($assertionsDisabled || !address.isUnresolved()) {
            post(new Runnable() {
                public void run() {
                    IOException e;
                    if (!cancel.isCancelled()) {
                        cancel.callback = callback;
                        SelectionKey ckey = null;
                        SocketChannel socket = null;
                        try {
                            ConnectFuture connectFuture = cancel;
                            SocketChannel socket2 = SocketChannel.open();
                            connectFuture.socket = socket2;
                            try {
                                socket2.configureBlocking(false);
                                ckey = socket2.register(AsyncServer.this.mSelector.getSelector(), 8);
                                ckey.attach(cancel);
                                socket2.connect(address);
                                socket = socket2;
                            } catch (IOException e2) {
                                e = e2;
                                socket = socket2;
                                if (ckey != null) {
                                    ckey.cancel();
                                }
                                StreamUtility.closeQuietly(socket);
                                cancel.setComplete((Exception) e);
                            }
                        } catch (IOException e3) {
                            e = e3;
                            if (ckey != null) {
                                ckey.cancel();
                            }
                            StreamUtility.closeQuietly(socket);
                            cancel.setComplete((Exception) e);
                        }
                    }
                }
            });
            return cancel;
        }
        throw new AssertionError();
    }

    public Cancellable connectSocket(final InetSocketAddress remote, final ConnectCallback callback) {
        if (!remote.isUnresolved()) {
            return connectResolvedInetSocketAddress(remote, callback);
        }
        final Cancellable ret = new SimpleFuture();
        Cancellable lookup = getByName(remote.getHostName());
        ret.setParent(lookup);
        lookup.setCallback(new FutureCallback<InetAddress>() {
            public void onCompleted(Exception e, InetAddress result) {
                if (e != null) {
                    callback.onConnectCompleted(e, null);
                    ret.setComplete(e);
                    return;
                }
                ret.setComplete(AsyncServer.this.connectResolvedInetSocketAddress(new InetSocketAddress(result, remote.getPort()), callback));
            }
        });
        return ret;
    }

    public Cancellable connectSocket(String host, int port, ConnectCallback callback) {
        return connectSocket(InetSocketAddress.createUnresolved(host, port), callback);
    }

    private static ExecutorService newSynchronousWorkers() {
        return new ThreadPoolExecutor(1, 4, 10, TimeUnit.SECONDS, new LinkedBlockingQueue(), new NamedThreadFactory("AsyncServer-worker-"));
    }

    public Future<InetAddress[]> getAllByName(final String host) {
        final SimpleFuture<InetAddress[]> ret = new SimpleFuture();
        synchronousWorkers.execute(new Runnable() {
            public void run() {
                try {
                    final InetAddress[] result = InetAddress.getAllByName(host);
                    if (result == null || result.length == 0) {
                        throw new HostnameResolutionException("no addresses for host");
                    }
                    AsyncServer.this.post(new Runnable() {
                        public void run() {
                            ret.setComplete(null, result);
                        }
                    });
                } catch (final Exception e) {
                    AsyncServer.this.post(new Runnable() {
                        public void run() {
                            ret.setComplete(e, null);
                        }
                    });
                }
            }
        });
        return ret;
    }

    public Future<InetAddress> getByName(String host) {
        return (Future) getAllByName(host).then(new TransformFuture<InetAddress, InetAddress[]>() {
            protected void transform(InetAddress[] result) throws Exception {
                setComplete(result[0]);
            }
        });
    }

    private boolean addMe() {
        synchronized (mServers) {
            if (((AsyncServer) mServers.get(this.mAffinity)) != null) {
                return false;
            }
            mServers.put(this.mAffinity, this);
            return true;
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void run(boolean r7) {
        /*
        r6 = this;
        r2 = 0;
        monitor-enter(r6);
        r4 = r6.mSelector;	 Catch:{ all -> 0x001f }
        if (r4 == 0) goto L_0x002e;
    L_0x0006:
        r4 = "NIO";
        r5 = "Reentrant call";
        android.util.Log.i(r4, r5);	 Catch:{ all -> 0x001f }
        r4 = $assertionsDisabled;	 Catch:{ all -> 0x001f }
        if (r4 != 0) goto L_0x0022;
    L_0x0011:
        r4 = java.lang.Thread.currentThread();	 Catch:{ all -> 0x001f }
        r5 = r6.mAffinity;	 Catch:{ all -> 0x001f }
        if (r4 == r5) goto L_0x0022;
    L_0x0019:
        r4 = new java.lang.AssertionError;	 Catch:{ all -> 0x001f }
        r4.<init>();	 Catch:{ all -> 0x001f }
        throw r4;	 Catch:{ all -> 0x001f }
    L_0x001f:
        r4 = move-exception;
        monitor-exit(r6);	 Catch:{ all -> 0x001f }
        throw r4;
    L_0x0022:
        r2 = 1;
        r3 = r6.mSelector;	 Catch:{ all -> 0x001f }
        r1 = r6.mQueue;	 Catch:{ all -> 0x001f }
    L_0x0027:
        monitor-exit(r6);	 Catch:{ all -> 0x001f }
        if (r2 == 0) goto L_0x0082;
    L_0x002a:
        runLoop(r6, r3, r1);	 Catch:{ AsyncSelectorException -> 0x0070 }
    L_0x002d:
        return;
    L_0x002e:
        r3 = new com.koushikdutta.async.SelectorWrapper;	 Catch:{ IOException -> 0x005d }
        r4 = java.nio.channels.spi.SelectorProvider.provider();	 Catch:{ IOException -> 0x005d }
        r4 = r4.openSelector();	 Catch:{ IOException -> 0x005d }
        r3.<init>(r4);	 Catch:{ IOException -> 0x005d }
        r6.mSelector = r3;	 Catch:{ IOException -> 0x005d }
        r1 = r6.mQueue;	 Catch:{ IOException -> 0x005d }
        if (r7 == 0) goto L_0x0060;
    L_0x0041:
        r4 = new com.koushikdutta.async.AsyncServer$13;	 Catch:{ all -> 0x001f }
        r5 = r6.mName;	 Catch:{ all -> 0x001f }
        r4.<init>(r5, r3, r1);	 Catch:{ all -> 0x001f }
        r6.mAffinity = r4;	 Catch:{ all -> 0x001f }
    L_0x004a:
        r4 = r6.addMe();	 Catch:{ all -> 0x001f }
        if (r4 != 0) goto L_0x0067;
    L_0x0050:
        r4 = r6.mSelector;	 Catch:{ Exception -> 0x0086 }
        r4.close();	 Catch:{ Exception -> 0x0086 }
    L_0x0055:
        r4 = 0;
        r6.mSelector = r4;	 Catch:{ all -> 0x001f }
        r4 = 0;
        r6.mAffinity = r4;	 Catch:{ all -> 0x001f }
        monitor-exit(r6);	 Catch:{ all -> 0x001f }
        goto L_0x002d;
    L_0x005d:
        r0 = move-exception;
        monitor-exit(r6);	 Catch:{ all -> 0x001f }
        goto L_0x002d;
    L_0x0060:
        r4 = java.lang.Thread.currentThread();	 Catch:{ all -> 0x001f }
        r6.mAffinity = r4;	 Catch:{ all -> 0x001f }
        goto L_0x004a;
    L_0x0067:
        if (r7 == 0) goto L_0x0027;
    L_0x0069:
        r4 = r6.mAffinity;	 Catch:{ all -> 0x001f }
        r4.start();	 Catch:{ all -> 0x001f }
        monitor-exit(r6);	 Catch:{ all -> 0x001f }
        goto L_0x002d;
    L_0x0070:
        r0 = move-exception;
        r4 = "NIO";
        r5 = "Selector closed";
        android.util.Log.i(r4, r5, r0);
        r4 = r3.getSelector();	 Catch:{ Exception -> 0x0080 }
        r4.close();	 Catch:{ Exception -> 0x0080 }
        goto L_0x002d;
    L_0x0080:
        r4 = move-exception;
        goto L_0x002d;
    L_0x0082:
        run(r6, r3, r1);
        goto L_0x002d;
    L_0x0086:
        r4 = move-exception;
        goto L_0x0055;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.koushikdutta.async.AsyncServer.run(boolean):void");
    }

    private static void run(AsyncServer server, SelectorWrapper selector, PriorityQueue<Scheduled> queue) {
        while (true) {
            try {
                runLoop(server, selector, queue);
            } catch (AsyncSelectorException e) {
                Log.e("NIO", "Selector exception", e);
                try {
                    selector.getSelector().close();
                } catch (Exception e2) {
                }
            }
            synchronized (server) {
                if (!selector.isOpen() || (selector.keys().size() <= 0 && queue.size() <= 0)) {
                    break;
                }
            }
        }
        shutdownEverything(selector);
        if (server.mSelector == selector) {
            server.mQueue = new PriorityQueue(1, Scheduler.INSTANCE);
            server.mSelector = null;
            server.mAffinity = null;
        }
        synchronized (mServers) {
            mServers.remove(Thread.currentThread());
        }
    }

    private static void shutdownKeys(SelectorWrapper selector) {
        try {
            for (SelectionKey key : selector.keys()) {
                StreamUtility.closeQuietly(key.channel());
                try {
                    ((SelectionKey) i$.next()).cancel();
                } catch (Exception e) {
                }
            }
        } catch (Exception e2) {
        }
    }

    private static void shutdownEverything(SelectorWrapper selector) {
        shutdownKeys(selector);
        try {
            selector.close();
        } catch (Exception e) {
        }
    }

    private static long lockAndRunQueue(AsyncServer server, PriorityQueue<Scheduled> queue) {
        long wait = Long.MAX_VALUE;
        while (true) {
            Scheduled run = null;
            synchronized (server) {
                long now = System.currentTimeMillis();
                if (queue.size() > 0) {
                    Scheduled s = (Scheduled) queue.remove();
                    if (s.time <= now) {
                        run = s;
                    } else {
                        wait = s.time - now;
                        queue.add(s);
                    }
                }
            }
            if (run == null) {
                return wait;
            }
            run.runnable.run();
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private static void runLoop(com.koushikdutta.async.AsyncServer r24, com.koushikdutta.async.SelectorWrapper r25, java.util.PriorityQueue<com.koushikdutta.async.AsyncServer.Scheduled> r26) throws com.koushikdutta.async.AsyncServer.AsyncSelectorException {
        /*
        r11 = 1;
        r0 = r24;
        r1 = r26;
        r20 = lockAndRunQueue(r0, r1);
        monitor-enter(r24);	 Catch:{ Exception -> 0x00bc }
        r15 = r25.selectNow();	 Catch:{ all -> 0x00b9 }
        if (r15 != 0) goto L_0x0025;
    L_0x0010:
        r19 = r25.keys();	 Catch:{ all -> 0x00b9 }
        r19 = r19.size();	 Catch:{ all -> 0x00b9 }
        if (r19 != 0) goto L_0x0026;
    L_0x001a:
        r22 = 9223372036854775807; // 0x7fffffffffffffff float:NaN double:NaN;
        r19 = (r20 > r22 ? 1 : (r20 == r22 ? 0 : -1));
        if (r19 != 0) goto L_0x0026;
    L_0x0023:
        monitor-exit(r24);	 Catch:{ all -> 0x00b9 }
    L_0x0024:
        return;
    L_0x0025:
        r11 = 0;
    L_0x0026:
        monitor-exit(r24);	 Catch:{ all -> 0x00b9 }
        if (r11 == 0) goto L_0x0035;
    L_0x0029:
        r22 = 9223372036854775807; // 0x7fffffffffffffff float:NaN double:NaN;
        r19 = (r20 > r22 ? 1 : (r20 == r22 ? 0 : -1));
        if (r19 != 0) goto L_0x00c5;
    L_0x0032:
        r25.select();	 Catch:{ Exception -> 0x00bc }
    L_0x0035:
        r14 = r25.selectedKeys();
        r9 = r14.iterator();
    L_0x003d:
        r19 = r9.hasNext();
        if (r19 == 0) goto L_0x018f;
    L_0x0043:
        r10 = r9.next();
        r10 = (java.nio.channels.SelectionKey) r10;
        r19 = r10.isAcceptable();	 Catch:{ CancelledKeyException -> 0x00b7 }
        if (r19 == 0) goto L_0x00ce;
    L_0x004f:
        r13 = r10.channel();	 Catch:{ CancelledKeyException -> 0x00b7 }
        r13 = (java.nio.channels.ServerSocketChannel) r13;	 Catch:{ CancelledKeyException -> 0x00b7 }
        r16 = 0;
        r5 = 0;
        r16 = r13.accept();	 Catch:{ IOException -> 0x00a1 }
        if (r16 == 0) goto L_0x003d;
    L_0x005e:
        r19 = 0;
        r0 = r16;
        r1 = r19;
        r0.configureBlocking(r1);	 Catch:{ IOException -> 0x00a1 }
        r19 = r25.getSelector();	 Catch:{ IOException -> 0x00a1 }
        r22 = 1;
        r0 = r16;
        r1 = r19;
        r2 = r22;
        r5 = r0.register(r1, r2);	 Catch:{ IOException -> 0x00a1 }
        r17 = r10.attachment();	 Catch:{ IOException -> 0x00a1 }
        r17 = (com.koushikdutta.async.callback.ListenCallback) r17;	 Catch:{ IOException -> 0x00a1 }
        r8 = new com.koushikdutta.async.AsyncNetworkSocket;	 Catch:{ IOException -> 0x00a1 }
        r8.<init>();	 Catch:{ IOException -> 0x00a1 }
        r19 = r16.socket();	 Catch:{ IOException -> 0x00a1 }
        r19 = r19.getRemoteSocketAddress();	 Catch:{ IOException -> 0x00a1 }
        r19 = (java.net.InetSocketAddress) r19;	 Catch:{ IOException -> 0x00a1 }
        r0 = r16;
        r1 = r19;
        r8.attach(r0, r1);	 Catch:{ IOException -> 0x00a1 }
        r0 = r24;
        r8.setup(r0, r5);	 Catch:{ IOException -> 0x00a1 }
        r5.attach(r8);	 Catch:{ IOException -> 0x00a1 }
        r0 = r17;
        r0.onAccepted(r8);	 Catch:{ IOException -> 0x00a1 }
        goto L_0x003d;
    L_0x00a1:
        r6 = move-exception;
        r19 = 1;
        r0 = r19;
        r0 = new java.io.Closeable[r0];	 Catch:{ CancelledKeyException -> 0x00b7 }
        r19 = r0;
        r22 = 0;
        r19[r22] = r16;	 Catch:{ CancelledKeyException -> 0x00b7 }
        com.koushikdutta.async.util.StreamUtility.closeQuietly(r19);	 Catch:{ CancelledKeyException -> 0x00b7 }
        if (r5 == 0) goto L_0x003d;
    L_0x00b3:
        r5.cancel();	 Catch:{ CancelledKeyException -> 0x00b7 }
        goto L_0x003d;
    L_0x00b7:
        r19 = move-exception;
        goto L_0x003d;
    L_0x00b9:
        r19 = move-exception;
        monitor-exit(r24);	 Catch:{ all -> 0x00b9 }
        throw r19;	 Catch:{ Exception -> 0x00bc }
    L_0x00bc:
        r6 = move-exception;
        r19 = new com.koushikdutta.async.AsyncServer$AsyncSelectorException;
        r0 = r19;
        r0.<init>(r6);
        throw r19;
    L_0x00c5:
        r0 = r25;
        r1 = r20;
        r0.select(r1);	 Catch:{ Exception -> 0x00bc }
        goto L_0x0035;
    L_0x00ce:
        r19 = r10.isReadable();	 Catch:{ CancelledKeyException -> 0x00b7 }
        if (r19 == 0) goto L_0x00e7;
    L_0x00d4:
        r8 = r10.attachment();	 Catch:{ CancelledKeyException -> 0x00b7 }
        r8 = (com.koushikdutta.async.AsyncNetworkSocket) r8;	 Catch:{ CancelledKeyException -> 0x00b7 }
        r18 = r8.onReadable();	 Catch:{ CancelledKeyException -> 0x00b7 }
        r0 = r24;
        r1 = r18;
        r0.onDataReceived(r1);	 Catch:{ CancelledKeyException -> 0x00b7 }
        goto L_0x003d;
    L_0x00e7:
        r19 = r10.isWritable();	 Catch:{ CancelledKeyException -> 0x00b7 }
        if (r19 == 0) goto L_0x00f8;
    L_0x00ed:
        r8 = r10.attachment();	 Catch:{ CancelledKeyException -> 0x00b7 }
        r8 = (com.koushikdutta.async.AsyncNetworkSocket) r8;	 Catch:{ CancelledKeyException -> 0x00b7 }
        r8.onDataWritable();	 Catch:{ CancelledKeyException -> 0x00b7 }
        goto L_0x003d;
    L_0x00f8:
        r19 = r10.isConnectable();	 Catch:{ CancelledKeyException -> 0x00b7 }
        if (r19 == 0) goto L_0x0178;
    L_0x00fe:
        r4 = r10.attachment();	 Catch:{ CancelledKeyException -> 0x00b7 }
        r4 = (com.koushikdutta.async.AsyncServer.ConnectFuture) r4;	 Catch:{ CancelledKeyException -> 0x00b7 }
        r16 = r10.channel();	 Catch:{ CancelledKeyException -> 0x00b7 }
        r16 = (java.nio.channels.SocketChannel) r16;	 Catch:{ CancelledKeyException -> 0x00b7 }
        r19 = 1;
        r0 = r19;
        r10.interestOps(r0);	 Catch:{ CancelledKeyException -> 0x00b7 }
        r16.finishConnect();	 Catch:{ IOException -> 0x0150 }
        r12 = new com.koushikdutta.async.AsyncNetworkSocket;	 Catch:{ IOException -> 0x0150 }
        r12.<init>();	 Catch:{ IOException -> 0x0150 }
        r0 = r24;
        r12.setup(r0, r10);	 Catch:{ IOException -> 0x0150 }
        r19 = r16.socket();	 Catch:{ IOException -> 0x0150 }
        r19 = r19.getRemoteSocketAddress();	 Catch:{ IOException -> 0x0150 }
        r19 = (java.net.InetSocketAddress) r19;	 Catch:{ IOException -> 0x0150 }
        r0 = r16;
        r1 = r19;
        r12.attach(r0, r1);	 Catch:{ IOException -> 0x0150 }
        r10.attach(r12);	 Catch:{ IOException -> 0x0150 }
        r19 = r4.setComplete(r12);	 Catch:{ Exception -> 0x0147 }
        if (r19 == 0) goto L_0x003d;
    L_0x0138:
        r0 = r4.callback;	 Catch:{ Exception -> 0x0147 }
        r19 = r0;
        r22 = 0;
        r0 = r19;
        r1 = r22;
        r0.onConnectCompleted(r1, r12);	 Catch:{ Exception -> 0x0147 }
        goto L_0x003d;
    L_0x0147:
        r6 = move-exception;
        r19 = new java.lang.RuntimeException;	 Catch:{ CancelledKeyException -> 0x00b7 }
        r0 = r19;
        r0.<init>(r6);	 Catch:{ CancelledKeyException -> 0x00b7 }
        throw r19;	 Catch:{ CancelledKeyException -> 0x00b7 }
    L_0x0150:
        r7 = move-exception;
        r10.cancel();	 Catch:{ CancelledKeyException -> 0x00b7 }
        r19 = 1;
        r0 = r19;
        r0 = new java.io.Closeable[r0];	 Catch:{ CancelledKeyException -> 0x00b7 }
        r19 = r0;
        r22 = 0;
        r19[r22] = r16;	 Catch:{ CancelledKeyException -> 0x00b7 }
        com.koushikdutta.async.util.StreamUtility.closeQuietly(r19);	 Catch:{ CancelledKeyException -> 0x00b7 }
        r19 = r4.setComplete(r7);	 Catch:{ CancelledKeyException -> 0x00b7 }
        if (r19 == 0) goto L_0x003d;
    L_0x0169:
        r0 = r4.callback;	 Catch:{ CancelledKeyException -> 0x00b7 }
        r19 = r0;
        r22 = 0;
        r0 = r19;
        r1 = r22;
        r0.onConnectCompleted(r7, r1);	 Catch:{ CancelledKeyException -> 0x00b7 }
        goto L_0x003d;
    L_0x0178:
        r19 = "NIO";
        r22 = "wtf";
        r0 = r19;
        r1 = r22;
        android.util.Log.i(r0, r1);	 Catch:{ CancelledKeyException -> 0x00b7 }
        r19 = new java.lang.RuntimeException;	 Catch:{ CancelledKeyException -> 0x00b7 }
        r22 = "Unknown key state.";
        r0 = r19;
        r1 = r22;
        r0.<init>(r1);	 Catch:{ CancelledKeyException -> 0x00b7 }
        throw r19;	 Catch:{ CancelledKeyException -> 0x00b7 }
    L_0x018f:
        r14.clear();
        goto L_0x0024;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.koushikdutta.async.AsyncServer.runLoop(com.koushikdutta.async.AsyncServer, com.koushikdutta.async.SelectorWrapper, java.util.PriorityQueue):void");
    }

    public Thread getAffinity() {
        return this.mAffinity;
    }

    public boolean isAffinityThread() {
        return this.mAffinity == Thread.currentThread();
    }
}
